package com.jason.campusinn;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.DisplayMetrics;
import android.view.GestureDetector;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Toast;

import com.jason.campusinn.view.LoadingDialog;
import com.lidroid.xutils.ViewUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Random;

public class BaseActivity extends ActionBarActivity {
    protected Context mContext;
    /**
     * 全局DbUtils对象
     */
//    protected DbUtils mDbUtils;

    /**
     * 手势监听
     */
    private GestureDetector mGestureDetector;
    /**
     * 是否需要监听手势关闭功能
     */
    private boolean mNeedBackGesture = false;
    /**
     * 屏幕宽度
     */
    public int mScreenWidth;

    /**
     * 屏幕高度
     */
    public int mScreenHeight;

    /**
     * loadingDialog
     */
    protected LoadingDialog mLoadingDialog;
    private Toast mToast;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
//        mDbUtils = WannaApp.dbUtils;
        mLoadingDialog = new LoadingDialog(this);
        CampusInnApp.getInstance().addActivity(this);
        DisplayMetrics metric = getApplicationContext().getResources().getDisplayMetrics();
        mScreenWidth = metric.widthPixels;
        mScreenHeight = metric.heightPixels;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }


    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        ViewUtils.inject(this);// xutils用注解方式绑定控件方法@ViewInject(R.id.button)
    }

    @Override
        public void setContentView(View view) {
        super.setContentView(view);
        ViewUtils.inject(this);// xutils用注解方式绑定控件方法@ViewInject(R.id.button)
    }

    @Override
    public void setContentView(View view, LayoutParams params) {
        super.setContentView(view, params);
        ViewUtils.inject(this);// xutils用注解方式绑定控件方法@ViewInject(R.id.button)
    }

    private void initGestureDetector() {
        if (mGestureDetector == null) {
            mGestureDetector = new GestureDetector(getApplicationContext(),
                    new BackGestureListener(this));
        }
    }


//    @Override
//    public boolean dispatchTouchEvent(MotionEvent ev) {
//        if (mNeedBackGesture) {
//            return mGestureDetector.onTouchEvent(ev)
//                    || super.dispatchTouchEvent(ev);
//        }
//        return super.dispatchTouchEvent(ev);
//    }

    /*
     * 设置是否需要左滑关闭当前页的手势操作
     */
    public void setNeedBackGesture(boolean mNeedBackGesture) {
        this.mNeedBackGesture = mNeedBackGesture;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        CampusInnApp.getInstance().finishActivity(this);
    }

    /**
     * 显示Toast提示
     *
     * @param text
     */
    protected void showToast(String text) {
        if (mContext == null){
            return;
        }
        if (mToast != null){
            mToast.setText(text);
        } else {
            mToast = Toast.makeText(mContext, text, Toast.LENGTH_SHORT);
        }
        mToast.show();
//        Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show();
    }

    /**
     * get the out_trade_no for an order. 生成商户订单号，该值在商户端应保持唯一（可自定义格式规范）
     */
    public String getOutTradeNo() {
        SimpleDateFormat format = new SimpleDateFormat("MMddHHmmss",
                Locale.getDefault());
        Date date = new Date();
        String key = format.format(date);

        Random r = new Random();
        key = key + r.nextInt();
        key = key.substring(0, 15);
        return key;
    }
}
